﻿<%@ WebHandler Language="C#" Class="DeductManage" %>
using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class DeductManage : AjaxHandlerBase
{
    public DeductManage()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_DeductManage_Export);
    }
    
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //添加报备相关信息
                case "getlist":
                    GetList(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private void GetList(HttpContext context)
    {
        try
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" (1=1) and BargainCompanyId='{0}' ",CurrentUserInfo.CompanyId);

            //查询条件
            //关键字
            if (!string.IsNullOrEmpty(context.Request["keyword"]))
                sb.AppendFormat(" and (b.ContractCode like '%{0}%' or b.HouseAddress like '%{0}%') ", context.Request["keyword"]);
            //类型
            if (!string.IsNullOrEmpty(context.Request["type"]))
                sb.AppendFormat(" and PreDeductType={0} ", context.Request["type"]);
            //抵扣时间
            if (!string.IsNullOrEmpty(context.Request["deducttime"])){
                string dt = context.Request["deducttime"];
                string[] list = dt.Split('.');
                sb.AppendFormat(" and a.PayArchDate='{0}' ", list[0]+'.'+list[1].TrimStart('0'));
            }    
            
            
            //条件
            string strWhere = sb.ToString();
            //页码
            int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            //页显示行数
            int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
            //欲选择字段列表 
            StringBuilder FieldList = new StringBuilder();
            FieldList.Append("RecordId,PayMoney,PreDeductMoney,PreDeductType,PayArchDate,ContractCode,HouseAddress,");
            FieldList.Append("VStanCommision,BStanCommision,VRealCommisionFinal,BRealCommisionFinal,VCommisionFinal,BCommisionFinal,");
            FieldList.Append("VConsultingFinal,BConsultingFinal");
            
            StringBuilder sbTableFieldList = new StringBuilder();
            sbTableFieldList.Append("a.RecordId,a.PayMoney,PreDeductMoney,PreDeductType,a.PayArchDate,b.ContractCode,b.HouseAddress,");
            sbTableFieldList.Append("VStanCommision,BStanCommision,VRealCommisionFinal,BRealCommisionFinal,VCommisionFinal,BCommisionFinal,");
            sbTableFieldList.Append("VConsultingFinal,BConsultingFinal");

            //所要查询的表名
            string TableName = "t_Finance_DeductHistory a left join t_Contract_BaseInfo b On A.ContractId = b.ContractId inner join t_Contract_CommisionInfo c on b.contractId = c.contractId ";
            //排序
            string strOrderField = "a.PayArchDate desc";
            if (!string.IsNullOrEmpty(context.Request["sort"]))
                strOrderField = context.Request["sort"];
            
            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetList(strPageIndex, strPageSize, strOrderField, TableName, strWhere, FieldList.ToString(), sbTableFieldList.ToString(), true);

            context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[1].Rows[0][0], Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0]))); 
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }    
}